import { combineReducers } from 'redux';
import * as types from './actionTypes';

// 往期功能操作
const previousReducer = (state = [], action) => {
  switch (action.type) {
    case types.ADD_PREVIOUS:
      return [action.data, ...state];
    case types.GET_PREVIOUS:
      return action.data;
    case types.DEL_PREVIOUS:
      return state.filter(item => item.key !== action.data.key);
    case types.EDIT_PREVIOUS:
      return state.map(item => item.key === action.data.key && (item = action.data));
    default:
      return state;
  }
}

// 研发项目操作
const devItemReducer = (state = [], action) => {
  switch (action.type) {
    case types.ADD_DEV_ITEM:
      return [...state, action.data];
    case types.DEL_DEV_ITEM:
      return state.filter(item => item.key !== action.data.key);
    case types.REVISE_DEV_ITEM:
    case types.PUBLISH_DEV_ITEM:
      return state.map(item => item.key === action.data.key ? action.data : item);
    case types.GET_DEV_ITEM_LIST:
      return action.data;
    default:
      return state;
  }
}

// 审核留言操作
const leaveMessageReducer = (state = [], action) => {
  switch (action.type) {
    case types.GET_LEAVE_MESSAGE_LIST:
      return action.data;
    case types.DEL_ONE_LEAVE_MESSAGE:
      return state.filter(item => item.key !== action.data.key);
    case types.CANCEL_ACCEPT_LEAVE:
    case types.ADD_ACCEPT_LEAVE:
      return state.map(item => item.key === action.data.key ? action.data : item);
    default:
      return state;
  }
}

const reducers = combineReducers({
  previousReducer,
  devItemReducer,
  leaveMessageReducer
});

export default reducers;